﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;

namespace WuYuHotel.WX_AccToken
{
    public class ResultNotify
    {
        public HttpRequestBase request { get; set; }
        public ResultNotify(HttpRequestBase HandleRequest)
        {
            this.request = HandleRequest;
        }

        public WxPayData GetNotifyData()
        {
            System.IO.Stream s = request.InputStream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            System.Diagnostics.Debug.WriteLine("收到微信支付回调数据:" +builder.ToString() );
            //Log.Info(this.GetType().ToString(), "Receive data from WeChat : " + builder.ToString());

            //转换数据格式并验证签名
            WxPayData data = new WxPayData();

            data.FromXml(builder.ToString());
            System.Diagnostics.Debug.WriteLine("Check sign success");
            return data;
        }

        public WxPayData MyProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();

            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                System.Diagnostics.Debug.WriteLine("The Pay result is error : " + res.ToXml());
                throw new WxPayException("The Pay result is error : " + res.ToXml());
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();

            //查询订单，判断订单真实性
            if (!QueryOrder(transaction_id))
            {
                //若订单查询失败，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                System.Diagnostics.Debug.WriteLine("Order query failure : " + res.ToXml());
                throw new WxPayException("Order query failure : " + res.ToXml());
                //page.Response.End();
            }
            //查询订单成功
            else
            {
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "SUCCESS");
                res.SetValue("return_msg", "OK");
                System.Diagnostics.Debug.WriteLine("order query success : " + res.ToXml());


                //这里最后改造函数为返回值
                return notifyData;
                //page.Response.Write(res.ToXml());
                //page.Response.End();
            }
        }


        public void ProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();

            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                System.Diagnostics.Debug.WriteLine("The Pay result is error : " + res.ToXml());
                throw new WxPayException("The Pay result is error : " + res.ToXml());
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();

            //查询订单，判断订单真实性
            if (!QueryOrder(transaction_id))
            {
                //若订单查询失败，则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                System.Diagnostics.Debug.WriteLine("Order query failure : " + res.ToXml());
                throw new WxPayException("Order query failure : " + res.ToXml());
                //page.Response.End();
            }
            //查询订单成功
            else
            {
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "SUCCESS");
                res.SetValue("return_msg", "OK");
                System.Diagnostics.Debug.WriteLine("order query success : " + res.ToXml());

                

                return;
                //page.Response.Write(res.ToXml());
                //page.Response.End();
            }
        }

        //查询订单
        private bool QueryOrder(string transaction_id)
        {
            WxPayData req = new WxPayData();
            req.SetValue("transaction_id", transaction_id);
            WxPayData res = WxPayApi.OrderQuery(req);
            if (res.GetValue("return_code").ToString() == "SUCCESS" &&
                res.GetValue("result_code").ToString() == "SUCCESS")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}